﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.Project.Entitys;
using Modules.Project.Repositories;
using Pipelines.Sockets.Unofficial.Arenas;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Exceptions;

namespace Modules.Project
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ProjectGuaranteeAccountService
    {
        private readonly ProjectGuaranteeAccountRepository repository;
        private readonly ProjectAttachmentService projectAttachmentService;
        private readonly IApplicationContext applicationContext;
        private readonly ILogger logger;

        public ProjectGuaranteeAccountService(ProjectGuaranteeAccountRepository repository, ProjectAttachmentService projectAttachmentService, IApplicationContext applicationContext, ILogger<ProjectGuaranteeAccountService> logger)
        {
            this.repository = repository;
            this.projectAttachmentService = projectAttachmentService;
            this.applicationContext = applicationContext;
            this.logger = logger;
        }

        public ProjectGuaranteeAccount? GetProjectGuaranteeAccount(int id)
        {
            return repository.Get(id);
        }

        public void Create(ProjectGuaranteeAccount entity, IEnumerable<string> attachments)
        {
            try
            {
                applicationContext.Transaction.Begin();
                repository.Insert(entity);

                #region 附件

                foreach (var item in attachments)
                {
                    ProjectAttachment projectAttachment = new()
                    {
                        Id = Guid.NewGuid().ToString().Replace("-", ""),
                        AttachmentId = item,
                        ObjectId = entity.Id.ToString(),
                        Tenant = 项目附件租户.ProjectGuaranteeAccount,
                        AttachmentType = 附件类型.营业执照,
                        CreateTime = DateTime.Now
                    };
                    projectAttachmentService.Create(projectAttachment);
                }

                #endregion 附件

                applicationContext.Transaction.Complete();
            }
            catch (Exception ex)
            {
                applicationContext.Transaction.Abort();
                logger.LogError(ex, $"创建保证金账户失败");
                throw new BusinessException("创建保证金账户失败");
            }
        }

        public void Update(ProjectGuaranteeAccount entity, IEnumerable<string> attachments)
        {
            try
            {
                applicationContext.Transaction.Begin();
                repository.Update(entity);

                #region 附件

                if (attachments.Count() > 0)
                {
                    List<ProjectAttachment> projectAttachments = projectAttachmentService.GetProjectAttachmentsByObjectIdAndType(entity.Id, 附件类型.营业执照, 项目附件租户.ProjectGuaranteeAccount).ToList();
                    projectAttachmentService.Deleted(projectAttachments.Select(i => i.Id));
                    foreach (var item in attachments)
                    {
                        ProjectAttachment projectAttachment = new()
                        {
                            Id = Guid.NewGuid().ToString().Replace("-", ""),
                            AttachmentId = item,
                            ObjectId = entity.Id.ToString(),
                            Tenant = 项目附件租户.ProjectGuaranteeAccount,
                            AttachmentType = 附件类型.营业执照,
                            CreateTime = DateTime.Now
                        };
                        projectAttachmentService.Create(projectAttachment);
                    }
                }

                #endregion 附件

                applicationContext.Transaction.Complete();
            }
            catch (Exception ex)
            {
                applicationContext.Transaction.Abort();
                logger.LogError(ex, $"修改保证金账户失败");
                throw new BusinessException("修改保证金账户失败");
            }
        }

        public IPagedList<ProjectGuaranteeAccount> GetProjectGuaranteeAccounts(ProjectGuaranteeAccountQuery query, int pageIndex, int pageSize)
        {
            return repository.GetProjectGuaranteeAccounts(query, pageIndex, pageSize);
        }

        public void Delete(int id)
        {
            repository.DeleteByEntityId(id);
        }

        public void Deleted(IEnumerable<int> ids)
        {
            foreach (int id in ids)
            {
                Delete(id);
            }
        }
    }
}